/*
 * PlayerSystem.cpp
 *
 *  Created on: Nov 6, 2012
 *      Author: root
 */

#include "PlayerSystem.h"

#include "Server.h"

PlayerSystem::PlayerSystem(Server * server)
:SystemAble<Server>(server)
{

	_core->insert_1(10001,std::bind(&PlayerSystem::on_simple_packet,this,std::placeholders::_1));

	_core->insert_2(10002,std::bind(&PlayerSystem::on_player_packet,this,std::placeholders::_1,std::placeholders::_2));

}

PlayerSystem::~PlayerSystem()
{
}

bool PlayerSystem::init()
{
	printf("PlayerSystem:%s\n",__FUNCTION__);

	return true;
}

bool PlayerSystem::uninit()
{
	printf("PlayerSystem:%s\n",__FUNCTION__);

	return true;
}

void PlayerSystem::add_player(const PlayerPtr & player)
{
	_players[player->get_id()] = player;
}

PlayerPtr PlayerSystem::get_player(int id)const
{
	auto it = _players.find(id);

	if(it == _players.end())
	{
		return PlayerPtr();
	}

	return it->second;
}

void PlayerSystem::on_simple_packet(const Packet & packet)
{
	printf("PlayerSystem:%s\n",__FUNCTION__);

	int pid = packet._pid;

	PlayerPtr player = PlayerPtr(new Player(pid));

	add_player(player);
}

void PlayerSystem::on_player_packet(const PlayerPtr & player,const Packet & packet)
{
	printf("PlayerSystem:%s\n",__FUNCTION__);
}

